
include('shared.lua')

local lineMat = Material( "cable/xbeam" )
local emitterDelay = 0.1

ENT.Emitter = nil
ENT.LastEmitted = 0
ENT.Colour = nil

function ENT:Initialize()

	self.Emitter = ParticleEmitter(self:GetPos())
	
end

function ENT:GetColour()
	return team.GetColor( self:GetNWInt( "team" ) )
end

function ENT:Think()

	if (self:GetOwner():IsValid()) then
	
		if (self.LastEmitted < CurTime()) then
		
			if ( self.Colour == team.GetColor(TEAM_UNASSIGNED) or self.Colour == nil ) then
				self.Colour = team.GetColor( self:GetNWInt( "team" ) )
			end

			if (self.Colour != nil) then
				local particle = self.Emitter:Add("sprites/light_glow02_add", self:GetPos()+(Vector(math.random(-4,4),math.random(-4,4), 0) * 3) - Vector(0,0,10))
				particle:SetColor(self.Colour.r, self.Colour.g, self.Colour.b, 255)
				particle:SetVelocity(Vector(math.random(-4,4),math.random(-4,4), 0):GetNormal() * 20)
				particle:SetGravity(Vector(0,0,-75))
				particle:SetDieTime(1.25)
				particle:SetLifeTime(0)
				particle:SetStartSize(25)
				particle:SetEndSize(0)
				particle:SetStartAlpha(255)
				particle:SetEndAlpha(0)
			end
		
			self.LastEmitted = CurTime() + emitterDelay
		end
	
	end

end

function ENT:Draw()

	self:DrawModel()
	self:SetColor( 255,255,255,255 )
	
	local ply = self:GetOwner()
	
	if ( ply != NULL and ply:IsValid() ) then
		
		if ( ply.HoldingItem and ply.HoldingItem:IsValid() ) then
		
			if ( LocalPlayer() == ply ) then
				self:SetColor( 255,255,255,255 )
			end
		
			local plypos = ( Vector(0,0,80) + ply:GetPos() )//:ToScreen()
			local proppos = ply.HoldingItem:GetPos()//:ToScreen()

			render.SetMaterial( lineMat )
			render.DrawBeam( plypos, proppos, 15, 0, 0, Color( 255, 255, 255, 255 ) )
		
		end
		
	end
	
end

